clear all
set more off 
set graphics on
set trace on
set tracedepth 2
set varabbrev off

macro drop _all

version 16
	
program run_project

	define_paths_globals	
	setup_directories
	clean_data
	descriptives 
	twfe
	govt_policy
	spec_choice
	pdv
	exhibits
	
end
	
program define_paths_globals

	* Main project paths
	cd ../
	gl mypath "`c(pwd)'"
	cd ../
	gl projectspace "`c(pwd)'"
	cd "$mypath"

	*******
	* Path to ADOs
	adopath + "${mypath}/code/ado"                 

	*******
	* Project subdirectories

	global build_data 		"$projectspace/data/build_data/main"
	global raw_data			"$projectspace/data/raw_data"
	global clean_data 		"$mypath/data/cleaned_tax_data"
	global intermediate_data	"$mypath/data/intermediate_data"
	global output	 		"$mypath/output"
	global temp	 		"$mypath/temp"
	global tables               	"$mypath/tables"
	global figures              	"$mypath/figures"
	global individ_figs         	"$mypath/figures/individ_figs"
	
end

program setup_directories 
	
	* Input directories
	
	cap mkdir "$projectspace/"
	cap mkdir "$projectspace/data"
	cap mkdir "$projectspace/data/build_data"
	cap mkdir "$projectspace/data/build_data/main" 	  // contains minimally processed Census data
	cap mkdir "$projectspace/data/raw_data"        	  // contains raw non-Census data

	* Intermediate data directories 

	cap mkdir "$mypath/data"
	cap mkdir "$mypath/data/cleaned_tax_data"

	cap mkdir "$mypath/data/intermediate_data"
	cap mkdir "$mypath/data/intermediate_data/crosswalks"
	cap mkdir "$mypath/data/intermediate_data/physician_compare"
	cap mkdir "$mypath/data/intermediate_data/physician_compare/intermediate"
	cap mkdir "$mypath/data/intermediate_data/medical_school_ranking"
	cap mkdir "$mypath/data/intermediate_data/medical_school_ranking/intermediate"
	cap mkdir "$mypath/data/intermediate_data/spouse_physician"
	cap mkdir "$mypath/data/intermediate_data/NRMP"
	cap mkdir "$mypath/data/intermediate_data/cz_characteristics"
	cap mkdir "$mypath/data/intermediate_data/specialty_characteristics"
	cap mkdir "$mypath/data/intermediate_data/MPUPD-PhysOtherSupplier"
	forvalues year = 2012/2017 {
		cap mkdir "$mypath/data/intermediate_data/MPUPD-PhysOtherSupplier/`year'"
	}
	cap mkdir "$mypath/data/intermediate_data/RVU"
	cap mkdir "$mypath/data/intermediate_data/twfe"
	cap mkdir "$mypath/data/intermediate_data/SAHIE"
	cap mkdir "$mypath/data/intermediate_data/ACA"
	cap mkdir "$mypath/data/intermediate_data/spec_choice"

	* Output
	cap mkdir "$mypath/output"
	cap mkdir "$mypath/output/descriptives"
	cap mkdir "$mypath/output/twfe"		
		cap mkdir "$mypath/output/twfe/geo_twfe"
			cap mkdir "$mypath/output/twfe/geo_twfe/physicians" 
			cap mkdir "$mypath/output/twfe/geo_twfe/lawyers"
		cap mkdir "$mypath/output/twfe/firm_twfe"
	cap mkdir "$mypath/output/spec_choice"
	cap mkdir "$mypath/output/pdv"
	
	* Temp directory
	cap mkdir "$mypath/temp"
	
	* CSV output directory 
	cap mkdir "$mypath/intermediate_csv"
	
	* Exhibits directories
	cap mkdir "$mypath/tables/"
	cap mkdir "$mypath/figures/"
		cap mkdir "$mypath/figures/individ_figs"
		
end

program clean_data

	run "$mypath/code/cleaning/docinc-clean01a-specialty_crosswalk.do"
	run "$mypath/code/cleaning/docinc-clean01b-geography_crosswalks.do"
	run "$mypath/code/cleaning/docinc-clean01c-MPUPD_import.do"	
	run "$mypath/code/cleaning/docinc-clean01d-physician_compare_import_clean.do"		
	run "$mypath/code/cleaning/docinc-clean01e-spouse_physician.do"
	run "$mypath/code/cleaning/docinc-clean01f-step_score_import_clean.do"
	run "$mypath/code/cleaning/docinc-clean01h-med_school.do"
	run "$mypath/code/cleaning/docinc-clean02a-tax_npi_clean.do"
	run "$mypath/code/cleaning/docinc-clean02b-tax_lawyer_clean.do"
	run "$mypath/code/cleaning/docinc-clean03a-cz_characteristics.do"
	run "$mypath/code/cleaning/docinc-clean03b-specialty_year_characteristics.do"
	
end

program descriptives

	run "$mypath/code/descriptives/docinc-desc01-sum_stats.do"
	run "$mypath/code/descriptives/docinc-desc02-distr.do"
	run "$mypath/code/descriptives/docinc-desc03_lifecycle.do"
	run "$mypath/code/descriptives/docinc-desc04-top_earners.do"
	run "$mypath/code/descriptives/docinc-desc05-specialty_stats.do"	
	run "$mypath/code/descriptives/docinc-desc06_appendix_tax_vs_ACS.do"
	run "$mypath/code/descriptives/docinc-desc07-time_series.do"
	run "$mypath/code/descriptives/docinc-desc08-firm_size.do"

end 


program twfe
	
	* Geographic decomposition

	run "$mypath/code/twfe/geo_twfe/docinc-geotwfe01a-main_analysis_physicians.do"
	run "$mypath/code/twfe/geo_twfe/docinc-geotwfe01b-main_analysis_lawyers.do"		
	run "$mypath/code/twfe/geo_twfe/docinc-geotwfe02-moverbias_correction.do"
	
	local path "$mypath/data/intermediate_data/twfe/"
	local pathr "$mypath/output/twfe/"
	shell python3 "$mypath/code/twfe/geo_twfe/docinc-geotwfe03-fe_estimator.py" `path' "`pathr'geo_twfe/"
	
	run "$mypath/code/twfe/geo_twfe/docinc-geotwfe04-descriptives.do"
	run "$mypath/code/twfe/geo_twfe/docinc-geotwfe05-correlating_czfe_physicians.do"
	
	* Firm decomposition

	run "$mypath/code/twfe/firm_twfe/docinc-firmtwfe01a-main_analysis_physicians.do"
	run "$mypath/code/twfe/firm_twfe/docinc-firmtwfe02-moverbias_correction.do"
	
	shell python3 "$mypath/code/twfe/firm_twfe/docinc-firmtwfe03-fe_estimator.py" `path' "`pathr'firm_twfe/"
	
	run "$mypath/code/twfe/docinc-twfe01-disclose_twfe.do"
	run "$mypath/code/descriptives/docinc-desc09-maps.do"

end

program govt_policy

	* RVU 

	run "$mypath/code/govt_policy/rvu/docinc-govtpolicy01a-RVU_import.do"			
	run "$mypath/code/govt_policy/rvu/docinc-govtpolicy01b-RVU_preparation.do" 
	run "$mypath/code/govt_policy/rvu/docinc-govtpolicy01c-RVU_regressions.do"
	run "$mypath/code/govt_policy/rvu/docinc-govtpolicy01d-cz_gaf_elasticity.do"

	* ACA

	run "$mypath/code/govt_policy/aca/docinc-govtpolicy02-ACA_regressions.do"

end 

program spec_choice

	run "$mypath/code/spec_choice/docinc-choice_model_OLS.do"
	run "$mypath/code/spec_choice/docinc-choice_model_RF.do"
	run "$mypath/code/spec_choice/docinc-choice_model_IV.do" 
	run "$mypath/code/spec_choice/docinc-choice_model_mean_spec_inc.do"
	run "$mypath/code/spec_choice/docinc-choice_correlating_top5.do"

end

program pdv

	run "$mypath/code/pdv/docinc-pdv01-transition_matrices.do"
	run "$mypath/code/pdv/docinc-pdv02-simulate_pdvs.do"
	run "$mypath/code/pdv/docinc-pdv03-pdv_means.do"
	run "$mypath/code/pdv/docinc-pdv04-pdv_hrs.do"
	run "$mypath/code/tuition/docinc-tuit01-average_tuition_and_fees.do"

end

program exhibits 
	
	* Exhibits common settings
	set scheme modern
	grstyle init
	grstyle set plain, horizontal
	grstyle set color blues, n(8) reverse
	graph set eps fontface "Times New Roman"

	* Figures and Tables
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_descriptives.do"
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_twfe.do"
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_aca.do"
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_rvu.do"
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_spec_choice.do"
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_pdv.do"
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_tax_policy.do"
	quietly texdoc do "$mypath/code/exhibits/docinc-exhibits_tex_for_figures.do"
	
	* PDF wrapper
	cd "$mypath/wrapper/"
	shell pdflatex -shell-escape -draftmode docinc-exhibits_wrapper.tex
	shell pdflatex docinc-exhibits_wrapper.tex
	capture shell rm docinc-exhibits_wrapper.aux
	capture shell rm docinc-exhibits_wrapper.log
	
end

*******
* Execute Project
run_project
